package cn.iocoder.yudao.module.water.controller.admin.proplan;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.water.controller.admin.proplan.vo.*;
import cn.iocoder.yudao.module.water.dal.dataobject.proplan.ProPlanDO;
import cn.iocoder.yudao.module.water.convert.proplan.ProPlanConvert;
import cn.iocoder.yudao.module.water.service.proplan.ProPlanService;

@Api(tags = "管理后台 - 计划数据")
@RestController
@RequestMapping("/water/pro-plan")
@Validated
public class ProPlanController {

    @Resource
    private ProPlanService proPlanService;

    @PostMapping("/create")
    @ApiOperation("创建计划数据")
    @PreAuthorize("@ss.hasPermission('water:pro-plan:create')")
    public CommonResult<Long> createProPlan(@Valid @RequestBody ProPlanCreateReqVO createReqVO) {
        return success(proPlanService.createProPlan(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新计划数据")
    @PreAuthorize("@ss.hasPermission('water:pro-plan:update')")
    public CommonResult<Boolean> updateProPlan(@Valid @RequestBody ProPlanUpdateReqVO updateReqVO) {
        proPlanService.updateProPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除计划数据")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('water:pro-plan:delete')")
    public CommonResult<Boolean> deleteProPlan(@RequestParam("id") Long id) {
        proPlanService.deleteProPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得计划数据")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('water:pro-plan:query')")
    public CommonResult<ProPlanRespVO> getProPlan(@RequestParam("id") Long id) {
        ProPlanDO proPlan = proPlanService.getProPlan(id);
        return success(ProPlanConvert.INSTANCE.convert(proPlan));
    }

    @GetMapping("/list")
    @ApiOperation("获得计划数据列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('water:pro-plan:query')")
    public CommonResult<List<ProPlanRespVO>> getProPlanList(@RequestParam("ids") Collection<Long> ids) {
        List<ProPlanDO> list = proPlanService.getProPlanList(ids);
        return success(ProPlanConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得计划数据分页")
    @PreAuthorize("@ss.hasPermission('water:pro-plan:query')")
    public CommonResult<PageResult<ProPlanRespVO>> getProPlanPage(@Valid ProPlanPageReqVO pageVO) {
        PageResult<ProPlanDO> pageResult = proPlanService.getProPlanPage(pageVO);
        return success(ProPlanConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出计划数据 Excel")
    @PreAuthorize("@ss.hasPermission('water:pro-plan:export')")
    @OperateLog(type = EXPORT)
    public void exportProPlanExcel(@Valid ProPlanExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProPlanDO> list = proPlanService.getProPlanList(exportReqVO);
        // 导出 Excel
        List<ProPlanExcelVO> datas = ProPlanConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "计划数据.xls", "数据", ProPlanExcelVO.class, datas);
    }

}
